package com.example.boos.stores.money;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;

import com.example.boos.HttpUtil.MyPost;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.pojo.Boss;
import com.example.boos.util.MyThreadPool;


public class balance_main extends AppCompatActivity {

    private TextView myBalance;
    private LinearLayout showBill;
    private CardView card;
    private BillsFragment billsFragment;
    private FragmentManager fm;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    Boolean ack = (Boolean) msg.obj;
                    if(ack){
                        // 设置余额
                        myBalance.setText(BossInfo.boss.getMoney().toString());
                    }else{
                        Toast.makeText(balance_main.this,"出现错误，请刷新！",Toast.LENGTH_SHORT).show();
                    }
                    break;

            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.back_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu_back:
                // finish()
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance_main);
        fm = getSupportFragmentManager();
        initView();
        initData();
        initListener();
    }

    private void initView(){
        myBalance = findViewById(R.id.balance_money);
        showBill = findViewById(R.id.balance_showBill);
        card = findViewById(R.id.balance_card);
    }

    private void initData(){
        // 获取到余额
        MyThreadPool.doJob(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 1;
                Boss boss = (Boss) MyPost.doPost("getBossById", BossInfo.bossId);
                msg.obj = false;
                if(boss != null){
                    BossInfo.boss = boss;
                    msg.obj = true;
                }
                handler.sendMessage(msg);
            }
        });
    }

    private void initListener(){
        // 为查看账单设置监听器
        showBill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示 Fragment
                showBillFragment();
            }
        });
    }

    private void showBillFragment(){
        card.setVisibility(View.GONE);
        billsFragment = new BillsFragment(callBack,this);
        fm.beginTransaction().add(R.id.balance_fragment,billsFragment).commitNow();
    }

    private void noShowBillFragment(){
        fm.beginTransaction().remove(billsFragment).commit();
        card.setVisibility(View.VISIBLE);
    }



    private BillsFragmentCallBack callBack = new BillsFragmentCallBack() {
        @Override
        public void back() {
            noShowBillFragment();
        }
    };
}